package com.gmail.hh.flickrj.sample.android.helper;

import com.gmail.hh.flickrj.sample.android.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.*;
import android.util.Log;


/**
 * get and put Preferences
 * 
 * @author helmut
 * 
 */
public class SettingsManager  {
	
	// Singleton instance
	private static SettingsManager instance;
	private static Context context;
	private static SharedPreferences prefs;
	private static Editor editor;
	
	// these hardcoded default are only used 
	// when there are no valid defaults in the xml-file preferences.xml
	private final String  DFT_SERVERURL 			= "http://bouldermapserver2xxxxxx.appspot.com/";
	private final Integer DFT_ROTATIONSTEPS 		= Integer.valueOf(90);
	private final Float   DFT_ROTATIONACCELERATION 	= Float.valueOf(1.0f);
	private final String  DFT_IMAGECENTERROT  		= "false";
	private final Integer DFT_JPEGCOMPRESSION 		= Integer.valueOf(51);
	
	// Constructor protected
	protected SettingsManager(){
	}

	// get an instance of Singleton
	public static SettingsManager getInstance() {
		if (SettingsManager.instance==null) {
			SettingsManager.instance = new SettingsManager();
		}
		return instance;
	}	
	
	
	// set the context   --- must be done once when instantiating first view and loading defaults
	public void setContext(Context context) {
		SettingsManager.context = context;
		SettingsManager.prefs = PreferenceManager.getDefaultSharedPreferences(context);
		SettingsManager.editor = SettingsManager.prefs.edit();
	}

	
// Server URL
	public String getServerUrl() {
		String serverUrlKey = context.getResources().getString(R.string.pref_serverUrl_key);
		String serverUrl = SettingsManager.prefs.getString(serverUrlKey, null);
		if (serverUrl==null) {  // hardcoded default - should never be used when config is ok
			serverUrl = new String(DFT_SERVERURL);
		}
		// Log.d(this.getClass().getSimpleName(), "SettingManager ServerUrl return:" + serverUrl);
		return serverUrl;
	}
	public void setServerUrl(String serverUrl) {
		String serverUrlKey = context.getResources().getString(R.string.pref_serverUrl_key);
		SettingsManager.editor.putString(serverUrlKey, serverUrl);
		SettingsManager.editor.commit();
	}


// Rotation Steps
	public int getRotationSteps() {
		String rotationStepsKey = context.getResources().getString(R.string.pref_rotationSteps_key);
		String rotationStepsString = SettingsManager.prefs.getString(rotationStepsKey, null);
		Integer rotationStepsInt = null;
		try {                        // 1. parse stored key/value pair
			rotationStepsInt = Integer.valueOf(rotationStepsString);
		} catch (NumberFormatException e) {   
			try {                     // 2. parse default value from xml-file 
				rotationStepsString = context.getResources().getString(R.string.pref_rotationSteps_default);
				rotationStepsInt = Integer.valueOf(rotationStepsString);
			} catch (NumberFormatException e2) {
				rotationStepsInt = DFT_ROTATIONSTEPS;  // 3. use hardcoded value
			}
		}
		int rotationSteps = rotationStepsInt.intValue();
		// only values between 1 and 90 allowed
		if (rotationSteps < 1) rotationSteps=1;   
		if (rotationSteps >90) rotationSteps=90;   
		return rotationSteps;
	}

	public void setRotationSteps(String rotationStepsString) {
		String rotationStepsKey = context.getResources().getString(R.string.pref_rotationSteps_key);
		SettingsManager.editor.putString(rotationStepsKey, String.valueOf(rotationStepsString) );
		SettingsManager.editor.commit();
	}
	
	
	// Rotation Acceleration
		public float getRotationAcceleration() {
			String rotationAccelerationKey = context.getResources().getString(R.string.pref_rotationAcceleration_key);
			String rotationAccelerationString = SettingsManager.prefs.getString(rotationAccelerationKey, null);
			Float rotationAccelerationFlt = null;
			try {                        // 1. parse stored key/value pair
				rotationAccelerationFlt = Float.valueOf(rotationAccelerationString);
			} catch (NumberFormatException e) {   
				try {                     // 2. parse default value from xml-file 
					rotationAccelerationString = context.getResources().getString(R.string.pref_rotationAcceleration_default);
					rotationAccelerationFlt = Float.valueOf(rotationAccelerationString);
				} catch (NumberFormatException e2) {
					rotationAccelerationFlt = DFT_ROTATIONACCELERATION;  // 3. use hardcoded value
				}
			}
			float rotationAcceleration = rotationAccelerationFlt.floatValue();
			// only values between min. and max. allowed
			if (rotationAcceleration < .25) rotationAcceleration=0.25f;   
			if (rotationAcceleration >4.0) rotationAcceleration=4.0f;   
			return rotationAcceleration;
		}

		public void setRotationAcceleration(String rotationAccelerationString) {
			String rotationAccelerationKey = context.getResources().getString(R.string.pref_rotationAcceleration_key);
			SettingsManager.editor.putString(rotationAccelerationKey, String.valueOf(rotationAccelerationString) );
			SettingsManager.editor.commit();
		}
	

// Image Einpassen, Zentrieren, Rotieren
		public String getImageCenterRot() {
			String imageCenterRotKey = context.getResources().getString(R.string.pref_imageCenterRot_key);
			String imageCenterRot = SettingsManager.prefs.getString(imageCenterRotKey, null);
			if (imageCenterRot==null) {  // hardcoded default - should never be used when config is ok
				imageCenterRot = new String(DFT_IMAGECENTERROT);
			}
			return imageCenterRot;
		}
		public void setImageCenterRot(String imageCenterRot) {
			String imageCenterRotKey = context.getResources().getString(R.string.pref_imageCenterRot_key);
			SettingsManager.editor.putString(imageCenterRotKey, imageCenterRot);
			SettingsManager.editor.commit();
		}


		// JPEG Compression
			public int getJpegCompression() {
				String jpegCompressionKey = context.getResources().getString(R.string.pref_jpegCompression_key);
				String jpegCompressionString = SettingsManager.prefs.getString(jpegCompressionKey, null);
				Integer jpegCompressionInt = null;
				try {                        // 1. parse stored key/value pair
					jpegCompressionInt = Integer.valueOf(jpegCompressionString);
				} catch (NumberFormatException e) {   
					try {                     // 2. parse default value from xml-file 
						jpegCompressionString = context.getResources().getString(R.string.pref_jpegCompression_default);
						jpegCompressionInt = Integer.valueOf(jpegCompressionString);
					} catch (NumberFormatException e2) {
						jpegCompressionInt = DFT_JPEGCOMPRESSION;  // 3. use hardcoded value
					}
				}
				int jpegCompression = jpegCompressionInt.intValue();
				// only values between 0 and 100 allowed
				if (jpegCompression < 0) jpegCompression=0;   
				if (jpegCompression >100) jpegCompression=100;   
				return jpegCompression;
			}

			public void setjpegCompressionpegCompression(String jpegCompressionString) {
				String jpegCompressionKey = context.getResources().getString(R.string.pref_jpegCompression_key);
				SettingsManager.editor.putString(jpegCompressionKey, String.valueOf(jpegCompressionString) );
				SettingsManager.editor.commit();
			}
		

}
